<?php
/**
 * @desc 后台用户表数据层
 * @author kevin.wang
 * @date 2015-01-01 12:00:00
 */
class Admin extends Base
{
	//数据表
	protected $table = 'pert_admin_user';
	//用户登录查询
	public function userLogin($username)  
    {
		 $table = $this->table;
		 $filed = "`admin_user_id`,`username`,`password`,`realname`,`mobile`,`email`,`addr`,`login_count`,`login_time`,`login_ip`";
		 $sql = "select {$filed} from `{$table}` where `username` = :username and `delete_flag` = 0";
		 $binds = array(":username" => $username);
		 return $this->select_table($sql, $binds, Base::QUERY_RAW);
    }
	//用户登录次数统计
	public function userLoginCount($username){
		$table = $this->table;
		$ip = Yii::app()->request->userHostAddress;
		$sql = "update `{$table}` set `login_count` = `login_count` + 1, `login_ip` = :ip, `login_time` = NOW() where `username` = :username and `delete_flag` = 0";
		$binds = array(":username" => $username, ':ip' => $ip);
		return $this->update_table($sql, $binds);
	}
	//获取用户信息
	public function getUserInfo($userId){
		$table = $this->table;
		$filed = "`admin_user_id`,`username`,`password`,`realname`,`mobile`,`email`,`addr`,`login_count`,`login_time`,`login_ip`";
		$sql = "select {$filed} from `{$table}` where `user_id` = :user_id and `delete_flag` = 0";
		$binds = array(":user_id" => $userId);
		return $this->select_table($sql, $binds, Base::QUERY_RAW);
	}
	//修改用户密码
	public function modifyPasswd($password, $user_id){
		$table = $this->table;
		$sql = "update `{$table}` set `password` = :password where `admin_user_id` = :admin_user_id and `delete_flag` = 0";
		return $this->update_table($sql, array(':password'=>$password, ':admin_user_id'=>$user_id));
	}
	//查询系统用户列表
	public function getAdminList(array $con)  
    {
		 $table = $this->table;
		 $filed = "`admin_user_id`,`username`,`realname`,`password`,`email`,`mobile`,`addr`,`login_ip`,`login_time`,`login_count`,`delete_flag`,`raw_add_time`,`raw_update_time`";
		 $sql = "select {$filed} from `{$table}` where `delete_flag` = 0";
		 $where = "";
		 if(!empty($con['username'])){
			 $where .= " and `username` like '%{$con['username']}%'";
		 }
		 if(!empty($con['email'])){
			 $where .= " and `email` like '%{$con['email']}%'";
		 }
		 if(!empty($con['mobile'])){
			 $where .= " and `mobile` like '%{$con['mobile']}%'";
		 }
		 $limit = " order by `admin_user_id` desc limit ".(($con['current'] - 1)*$con['numPerPage']).", ".$con['numPerPage'];
		 $return = $this->select_table($sql.$where.$limit, array());
		 $sql = "select count(`admin_user_id`) as num from `{$table}` where `delete_flag` = 0";
		 $return[] = $this->select_table($sql.$where, array(), Base::QUERY_COLUMN);
		 return $return;
    }
	//删除系统用户
	public function deleteAdmin($ids){
		$table = $this->table;
		$sql = "update `{$table}` set `delete_flag` = 1 where `admin_user_id` in ({$ids}) and `username` <> 'admin'";
		return $this->update_table($sql, array());
	}
	//写入系统用户
	public function insertAdmin(array $data){
		if(empty($data)){
			return false;
		}
		return self::insert_table($data, $this->table);
	}
	//获取系统用户
	public function getAdmin($id){
		$table = $this->table;
		$filed = "`admin_user_id`,`username`,`realname`,`password`,`email`,`mobile`,`addr`,`login_ip`,`login_time`,`login_count`,`delete_flag`,`raw_add_time`,`raw_update_time`";
		$sql = "select {$filed} from `{$table}` where `admin_user_id` = :admin_user_id and `delete_flag` = 0";
		return $this->select_table($sql, array(':admin_user_id'=>$id), Base::QUERY_RAW);
	}
	//修改系统用户
	public function editAdmin($id, array $data){
		if(empty($id) || empty($data)){
			return false;
		}
		$table = $this->table;
		$sql = "update `{$table}` set ";
		$binds = array();
		foreach($data as $key=>$value){
			$sql .= "`{$key}` = :{$key},";
			$binds[":{$key}"] = $value;
		}
		$sql = trim($sql, ',');
		$sql .= " where `admin_user_id` = :admin_user_id";
		$binds[':admin_user_id'] = $id;
		return $this->update_table($sql, $binds);
	}
}
